package com.hougong.im.httpcontroller;

import com.hougong.im.entity.ChatUser;
import com.hougong.im.entity.Friend;
import com.hougong.im.mapper.ApplyAddfriendMapper;
import com.hougong.im.mapper.ChatUserMapper;
import com.hougong.im.mapper.FriendMapper;
import com.hougong.im.mapper.PersonMessageMapper;
import com.hougong.im.utils.JwtUtils;
import com.hougong.im.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private ChatUserMapper chatUserMapper;
    @Autowired
    private FriendMapper friendMapper;

    @RequestMapping("/delete")
    public R deleteFriend(HttpServletRequest request){
        String token = request.getParameter("token");
        Long tokenUid = JwtUtils.getTokenUid(token);
        Long fid = Long.valueOf(request.getParameter("fid"));
        if(fid!=null){
            Friend friend = friendMapper.isFriend(tokenUid, fid);
            if(friend!=null){
                int i = friendMapper.deleteFriend(tokenUid, fid);
                return R.ok();
            }
        }
        return R.error();
    }

    @RequestMapping("/remark")
    public R remark(HttpServletRequest request){
        String token = request.getParameter("token");
        Long tokenUid = JwtUtils.getTokenUid(token);
        Long fid = Long.valueOf(request.getParameter("fid"));
        String remark = request.getParameter("remark");
        if(fid!=null){
            Friend friend = friendMapper.isFriend(tokenUid, fid);
            if(friend!=null){
                ChatUser f_chatUser = chatUserMapper.selectById(fid);
                if(remark==null || remark.equals("")){
                    remark=f_chatUser.getNickname();
                }
                friend.setfRemark(remark);
                friendMapper.updateById(friend);
                return R.ok();
            }
        }
        return R.error();
    }

}
